package com.rong.action;
 
 import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
 import com.opensymphony.xwork2.ActionContext;
 import com.opensymphony.xwork2.ActionSupport;
import com.rong.ORM.ZynTransfer;
 import com.rong.ORM.ZynUserRight;
import com.rong.DAO.ZynUserRightDAO;
public class UserRightAction extends ActionSupport {

    private ZynUserRightDAO userDao = new ZynUserRightDAO();

    private ZynUserRight right;    //注册提交的用户对     
    private List<ZynUserRight> rightlist;
	private String saveFlag="";
	private int id=-1; // the car id indatabase.
	private int userId;
  

	/**
	 * @return the userId
	 */
	public int getUserId() {
		return userId;
	}

	/**
	 * @param userId the userId to set
	 */
	public void setUserId(int userId) {
		this.userId = userId;
	}

	/**
	 * @return the right
	 */
	public ZynUserRight getRight() {
		return right;
	}

	/**
	 * @param right the right to set
	 */
	public void setRight(ZynUserRight right) {
		this.right = right;
	}

	
	/**
	 * @return the rightlist
	 */
	public List<ZynUserRight> getRightlist() {
		return rightlist;
	}

	/**
	 * @param rightlist the rightlist to set
	 */
	public void setRightlist(List<ZynUserRight> rightlist) {
		this.rightlist = rightlist;
	}

	/**
	 * @return the saveFlag
	 */
	public String getSaveFlag() {
		return saveFlag;
	}

	/**
	 * @param saveFlag the saveFlag to set
	 */
	public void setSaveFlag(String saveFlag) {
		this.saveFlag = saveFlag;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	@Override
    public String execute() throws Exception {

		for(Iterator<ZynUserRight> iterUR=rightlist.iterator();iterUR.hasNext();){
			ZynUserRight urObj=(ZynUserRight)iterUR.next();
			if(urObj==null){continue;}
			urObj.setUserId(this.getUserId());
			System.out.println("====userId:"+urObj.getUserId()+"menuID:"+urObj.getMenuId());
			userDao.save(urObj);
		}
        return "success";
    }
	
	
//    public String findall() throws Exception {
//        
//		 this.setList(userDao.findAll());        //保存注册的user对象
//	        return "success_list";
//	    }
    
	 public String findbyid() throws Exception {
	       
		this.setRightlist(userDao.findByUserId(this.getUserId()));// find the car by id			
	        return "s_find_one";
	       
	    }
	 public String edit() throws Exception {
		 
		 for(Iterator<ZynUserRight> iterUR=rightlist.iterator();iterUR.hasNext();){
				ZynUserRight urObj=(ZynUserRight)iterUR.next();
				if(urObj!=null&&urObj.getId()!=null&&urObj.getMenuId()==null){  //删除权限
					System.out.println("[UserRightAction]= delete ==userId:"+urObj.getId());
					userDao.delete(userDao.findById(urObj.getId()));					
				}else{
					if(urObj==null){continue;}
					urObj.setUserId(this.getUserId());
					System.out.println("[UserRightAction]=insert or update ==userId:"+urObj.getUserId()+"menuID:"+urObj.getMenuId());
					userDao.merge(urObj);					
				}
				
			}
		    return "s_edit_one";
		    }
//	 public String delete() throws Exception {
//	        
//			userDao.delete(userDao.findById(this.getId()));// delete the car by id			
//		    return "s_delete_one";
//		    }
//	
	 
    @Override
    public void validate() {
       //验证码验证
//        if(null == inputCertCode || "".equals(inputCertCode)){
//            this.addFieldError("inputCertCode", "验证码不能为空!");
//        }else{
//            Map session = ActionContext.getContext().getSession();
//            String certCode = session.get("certCode").toString();
//            if( ! certCode.equals(this.inputCertCode)){
//                this.addFieldError("inputCertCode", "验证码不正确!");
//            }
 //       }
        
    }
        
}
